<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */
defined('BY_JYA') or exit('error');
$_AR['wx']=pdo_get('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1),array('id','appid','appsecret','type','oldid','is_open'));
if(!$_AR['wx']){
	$_AR['wx']=pdo_get('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),array('id','appid','appsecret','type','oldid','is_open'));
}
//借用
//开启借用系统公众号授权获取粉丝信息
if(!$_SESSION['wechat_id'] && !$_SESSION['auth_wxid']){
	$uni_auth=uni_setting('wechat_auth');
	$core_auth=core_setting('wechat_auth');
	if(!$core_auth['wechat_id']){
		$core_auth['wechat_id']=pdo_getcolumn('core_account_wechat',array('uniacid'=>1,'isdefault'=>1),'id');
		if(!$core_auth['wechat_id']){
			$core_auth['wechat_id']=pdo_getcolumn('core_account_wechat',array('uniacid'=>1),'id');
		}
	}
	if($core_auth['wechat_id'] && $_AR['wx']['type']!=4 && $_AR['wx']['is_open'] && $core_auth['open'] ==1){
		$_SESSION['auth_wxid']=$core_auth['wechat_id'];
	}else{
		$_SESSION['auth_wxid']=0;
	}
}
//判断是否来自H5app,openid清空
if(($_RQ['fromh5wxapp'] && !$_SESSION['isopenid']) || ($_SESSION['openid'] && $_SESSION['wechatopenid'] && $_SESSION['wechatopenid']!=$_SESSION['openid'])){
	$_SESSION['isopenid']=true;
	$getopenid=1;
	$_SESSION['openid']='';
	$_SESSION['isauth_base']='';
}
if($_AR['wx'] || $_SESSION['auth_wxid']){//取消此处获取粉丝信息
	$_SESSION['wechat_id']=$_AR['wx']['id'];
	//如果是普通公众号，就借用系统公众号，如果本公众号获取粉丝失败，也借用
	if((($_SESSION['wechat_id'] && $_AR['wx']['type']==4) || $_SESSION['auth_wxid'])){
		$_SESSION['after_auth_url']=$_AR['siteurl'];
		if(!$_SESSION['uid'] || $getopenid || $_SESSION['isauth_info']!=1){
			$_SESSION['after_auth_url']=$_AR['siteurl'];
			//不存在openid时，第一次获取openid
			if(!$_SESSION['openid'] && $_SESSION['isauth_base']!=1){
					$url=model('core/member')->wechat_auth_base_url();
					if($url){
						header('location:'.$url);exit;
					}
			}elseif($_SESSION['openid'] && $_SESSION['isauth_info']!=1 && $_SESSION['fromh5wxapp']!=1){//已经有openid时，第一次获取用户信息，非H5小程序
					$setting=uni_setting('fans');
					if($setting['type']!=1){
						$url=model('core/member')->wechat_auth_info_url();
						if($url){
							header('location:'.$url);exit;
						}
					}
			}
		}
	}
	//读取jssdk 如果是普通公众号，就借用系统公众号，如果本公众号获取粉丝失败，也借用
	
	if($_AR['wx']['type']==2 || $_AR['wx']['type']==4 || $_SESSION['auth_wxid']){//为了便于显示，open不等于1，开启！
		if($_AR['wx']['type']!=2 && $_AR['wx']['type']!=4 ){
			$_AR['jssdk']=cfc('account',$_AR['wx']['id'])->getJssdkConfig();
		}else{
			$_AR['jssdk']=cfc('account',$_SESSION['auth_wxid'])->getJssdkConfig();
		}
		$_AR['jssdk']=ijson_encode($_AR['jssdk']);
		$_AR['share']=array(
			'title'=>$_SESSION['account']['title'],
			'desc'=>$_SESSION['account']['desc'] ? $_SESSION['account']['desc'] : $_SESSION['account']['title'],
			'link'=>$_AR['siteurl'].'&fromuser='.$_SESSION['uid'],
			'imgUrl'=>$_SESSION['account']['logo'] ? tomedia($_SESSION['account']['logo']) : SITEROOT.'core/resource/images/nopic.jpg',
		);
		$_AR['share']=ijson_encode($_AR['share']);
	}
}

